
const dayjs = require('dayjs');
import "./zh-cn";
dayjs.locale('zh-cn')
/**
 * 获取时间范围数据
 * @param {*} minDate 最小可选的日期，不传则默认今天
 * @param {*} maxDate 最大可选的日期，不传则默认一个月后
 */
const getRangeTime = (minDate, maxDate) => {
  const resArr = []
  const dates = getDatesBetween(minDate, maxDate)
  dates.forEach((item) => {
    resArr.push({
      year: dayjs(item).format("YYYY"),
      monthDay: dayjs(item).format('MMMD'),
      week: dayjs(item).format('ddd')
    })
  })
  return resArr
}

const getDatesBetween = (startDate = dayjs(), endDate = dayjs().add(1, 'month'), format = 'YYYY-MM-DD') => {
  const dates = [];
  let currentDate = dayjs(startDate);
  while (currentDate.isBefore(endDate) || currentDate.isSame(endDate)) {
    dates.push(currentDate.format(format));
    currentDate = currentDate.add(1, 'day');
  }
  return dates;
}

const formatTime = (currentDate = new Date(), format = 'YYYY-MM-DD') => dayjs(currentDate).format(format)

const getWeek = (date) => dayjs(date).day()

/**
 * 获取当前日期函数
 */
const getNowFormatDate = (format = 'YYYY-M-D') => {
  const [year, month, day] = formatTime(new Date(), format).split('-')
  return {
    year, month, day
  }
}
const daysDiff = (start, end) => dayjs(end).diff(dayjs(start), 'day');



module.exports = {
  formatTime,
  getRangeTime,
  getNowFormatDate,
  getWeek,
  daysDiff,
  getDatesBetween
}

